<template>
  <div class="edit-password">
    <input password type="text" v-model="oldPassword" :placeholder="i18n('oldPassword','请输入原密码')" />
    <input password type="text" v-model="password" :placeholder="i18n('password','请输入密码')"/>
    <input password type="text" v-model="confirmPassword" :placeholder="i18n('confirmPassword','请输入确认密码')" />
    <button type="primary" @click="editPassword">{{i18n('ok','确认')}}</button>
  </div>
</template>

<script>
import LoginApi from "@/api/login";
import md5 from "js-md5";
import {sm2} from 'sm-crypto';

export default {
  name: "EditPassword",
  data(){
    return{
      oldPassword:'',//原密码
      password:'',//密码
      confirmPassword:'',//确认密码
      userId:'',
      //密码模式
      passwordmode: "default",
      //国产sm2公钥
      sw2PublicKey:"040E279B33CCEDCAB5AB368BEA4243C7A0AABD0571819855A4C022E554065DCE68A6704974E611C63AA98534864BD1426969A5E8380F879AADD8EF14B595D277D5",
    }
  },
  created(){
      this.getLoginConfig();
    this.userId = uni.getStorageSync("user").userId
  },
  methods:{
      i18n(name, text, key) {
          return this.$ti18(name, text, "loginLang.editPassword", key);
      },
    //是否需要验证码
    getLoginConfig() {
      let self = this;
      LoginApi.getLoginConfig().then(resp => {
          self.passwordmode = resp.data.passwordmode;
      })
    },
    sm2Encrypt(password) {
      let pwd= sm2.doEncrypt(password, this.sw2PublicKey);
      return pwd.toString();
    },
    editPassword(){
      if(this.oldPassword ==""){
        uni.showToast({title: this.i18n('password','请输入密码'),icon: 'none'});
        return;
      }
      if(this.password ==""){
        uni.showToast({title: this.i18n('oldPassword','请输入原密码'), icon: 'none'});
        return;
      }
      if(this.confirmPassword ==""){
        uni.showToast({title: this.i18n('confirmPassword','请输入确认密码'),icon: 'none'});
        return;
      }
      if(this.password.trim()!=this.confirmPassword.trim()){
        uni.showToast({title: this.i18n('passwordConsistent','前后密码不一致'),icon: 'none'});
        return;
      }
      let params={
        userId:this.userId,
        newPwd:this.password,
        oldPwd:this.oldPassword
      }
      if ("md5" == this.passwordmode) {
          params.newPwd =md5(params.newPwd.trim())
          params.oldPwd =md5(params.oldPwd.trim())
      } else if ("sm2" == this.passwordmode) {
          params.isSm2Password ="YES";
          params.newPwd =this.sm2Encrypt(params.newPwd.trim())
          params.oldPwd =this.sm2Encrypt(params.oldPwd.trim())
      }
      LoginApi.updatePassword(params).then(res=>{
        if(res.success){
            uni.showToast({title: res.message,icon: 'none'});
            this.$router.back()
        }else{
          uni.showToast({title: res.message,icon: 'none'});
        }
      })
    }
  }
}
</script>

<style scoped>
.edit-password {
  background-color: white;
  overflow: hidden;
  height: 100%;
  display: flex;
  flex-direction: column;
  padding: 1rem;
}
.edit-password >>>input{
  margin: 1rem 0rem;
  border: 1px solid #d9d9d9;
  border-radius: 5px;
  padding: 0rem .5rem;
  height: 2.5rem;
}
.edit-password button{
  margin-top: 2.5rem;
  width: 100%;
}
</style>
